package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrderPageQueryDTO;
import com.sky.entity.Order;
import org.apache.ibatis.annotations.*;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderMapper {
    
    @Insert("insert into orders (number, product_name, order_amount, payment_method, order_status, " +
            "order_source, contact_person, contact_phone, customer_name, order_time, create_time, " +
            "update_time, amount, user_id, appointment_date) " +  // 移除 status 和 pay_status
            "values (#{number}, #{productName}, #{orderAmount}, #{paymentMethod}, #{orderStatus}, " +
            "#{orderSource}, #{contactPerson}, #{contactPhone}, #{customerName}, #{orderTime}, " +
            "#{createTime}, #{updateTime}, #{amount}, #{userId}, #{appointmentDate})")  // 移除 #{status} 和 #{payStatus}
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Order order);

    @Select("select * from orders")
    List<Order> list();

    // 使用XML配置，移除@Select注解
    Order getById(Long id);

    // 使用XML配置，移除@Select注解
    Order getByNumber(String number);

    @Update("UPDATE orders SET order_status = #{orderStatus}, update_time = #{updateTime} " +
            "WHERE number = #{number}")
    int updateStatus(@Param("number") String number, 
                     @Param("orderStatus") String orderStatus,
                     @Param("updateTime") LocalDateTime updateTime);

    /**
     * 分页查询
     */
    Page<Order> pageQuery(OrderPageQueryDTO orderPageQueryDTO);

    /**
     * 根据订单号查询订单ID
     * @param orderNumber 订单号
     * @return 订单ID
     */
    @Select("SELECT id FROM orders WHERE number = #{orderNumber}")
    Long getOrderIdByNumber(@Param("orderNumber") String orderNumber);

    @Update("UPDATE orders SET " +
            "product_name = #{productName}, " +
            "order_amount = #{orderAmount}, " +
            "payment_method = #{paymentMethod}, " +
            "order_status = #{orderStatus}, " +
            "order_source = #{orderSource}, " +
            "contact_person = #{contactPerson}, " +
            "contact_phone = #{contactPhone}, " +
            "appointment_date = #{appointmentDate}, " +  // 添加 appointment_date
            "update_time = #{updateTime} " +
            "WHERE id = #{id}")
    int updateOrder(Order order);

    @Update("UPDATE orders SET material_status = #{materialStatus} WHERE id = #{id}")
    int updateMaterialStatus(@Param("id") Long id, 
                           @Param("materialStatus") String materialStatus);
    
    @Update("UPDATE orders SET visa_status = #{visaStatus} WHERE id = #{id}")
    int updateVisaStatus(@Param("id") Long id, 
                        @Param("visaStatus") String visaStatus);
}